<template>
  <div class="content">
    <div class="header">
      <h3>{{ contentConfing.header?.title ?? '数据列表' }}</h3>
      <template v-if="contentConfing.header?.bthTitle && isCreate">
        <el-button type="primary" @click="newUserClick">
          {{ contentConfing.header?.bthTitle }}
        </el-button>
      </template>
    </div>

    <div class="table">
      <el-table
        :data="pageList"
        style="width: 100%"
        v-bind="contentConfing.childTree"
        empty-text="权限不足"
        border
        lazy
      >
        <template v-for="item in contentConfing.tableList" :key="item.prop">
          <template v-if="item.type === 'custom'">
            <el-table-column v-bind="item" align="center" show-overflow-tooltip="true">
              <template #default="scope">
                <slot :name="item.soltName" v-bind="scope"></slot>
              </template>
            </el-table-column>
          </template>

          <template v-else-if="item.type === 'handler'">
            <el-table-column
              align="center"
              :label="item.label"
              :width="item.width"
              v-if="isupdate && isDelete"
            >
              <template #default="scope">
                <el-button
                  v-if="isupdate"
                  type="primary"
                  size="small"
                  icon="Edit"
                  link
                  @click="handeleditBtnClick(scope.row)"
                >
                  编辑
                </el-button>
                <el-button
                  v-if="isDelete"
                  type="primary"
                  size="small"
                  icon="Delete"
                  link
                  @click="handelDeleteBtnClick(scope.row.id)"
                >
                  删除
                </el-button>
              </template>
            </el-table-column>
          </template>

          <template v-else>
            <el-table-column align="center" v-bind="item" show-overflow-tooltip="true" />
          </template>
        </template>
      </el-table>
    </div>

    <div class="pageination">
      <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[10, 20, 30]"
        layout="sizes, prev, pager, next, jumper,total,"
        :total="pageTotalCount"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, defineExpose, defineEmits, onUnmounted, defineProps } from 'vue'
import { storeToRefs } from 'pinia'
import { ElMessage } from 'element-plus'

import useStytem from '@/store/main/system/system'
import { useLoginStore } from '@/store/login/login'
import contentConfig from '@/views/main/system/menu/config/conunt.config'

interface IProps {
  contentConfing: {
    pageName: string
    childTree?: {
      rowKey: string
      treeProps: {
        children: string
      }
    }
    header?: {
      title?: string
      bthTitle?: string
    }
    tableList: any[]
  }
}

//根据用户的权限判断是否展示功能按钮
const loginStore = useLoginStore()

const isCreate = loginStore.permissions.find((item) =>
  item.includes(`${contentConfig.pageName}:create`)
)
const isDelete = loginStore.permissions.find((item) =>
  item.includes(`${contentConfig.pageName}:delete`)
)
const isQuery = loginStore.permissions.find((item) =>
  item.includes(`${contentConfig.pageName}:query`)
)
const isupdate = loginStore.permissions.find((item) =>
  item.includes(`${contentConfig.pageName}:update`)
)

const props = defineProps<IProps>()
const emits = defineEmits(['showModal', 'editModal'])
const systemStore = useStytem()
const currentPage = ref(1)
const pageSize = ref(10)

const { pageList, pageTotalCount } = storeToRefs(systemStore)
// 监听Action的监听
systemStore.$onAction(({ name }) => {
  if (
    name === 'deletePageByIdAction' ||
    name === 'createPageDataListAction' ||
    name === 'editPageDataListAction'
  ) {
    currentPage.value = 1
  }
})

// 发送网络请求
getPageListData()

// 页码
function handleSizeChange() {
  getPageListData()
}
function handleCurrentChange() {
  getPageListData()
}

// 将页码作为请求参数发送
function getPageListData(format?: any) {
  if (!isQuery) {
    return
  }
  const size = pageSize.value
  const offset = (currentPage.value - 1) * size
  systemStore.postPageListAction(props.contentConfing.pageName, { offset, size, ...format })
}

// 新建用户的事件
function newUserClick() {
  emits('showModal')
}
//删除按钮
async function handelDeleteBtnClick(id: number) {
  const result = await systemStore.deletePageByIdAction(props.contentConfing.pageName, id)
  if (result.code === 0) {
    ElMessage({
      message: '部门删除成功',
      type: 'success'
    })
  } else if (result.code === -1002) {
    ElMessage({
      message: '请不要删除测试部门',
      type: 'error'
    })
  }
}

//编辑按钮
function handeleditBtnClick(rowData: any) {
  emits('editModal', rowData)
}

// 切换用户是否禁用

// 每30s中重新请求列表数据

defineExpose({ getPageListData })
</script>

<style lang="scss" scoped>
.content {
  margin-top: 15px;
  padding: 20px;
  background-color: #fff;
  .header {
    display: flex;
    justify-content: space-between;
    margin-bottom: 10px;
  }
}
.table {
  :deep(.el-table__cell) {
    padding: 12px 0 !important;
  }
}
.pageination {
  display: flex;
  justify-content: flex-end;
  margin-top: 15px;
}
</style>
